home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bbsckit.zip / BBSCFILE.C < prev    next >
C/C++ Source or Header  |  1980-01-01  |  13KB  |  348 lines

  1. /*
  2.         bbscfile.c
  3.  
  4.         Support routines used by BBSc.c to do file i/o for the
  5.         message file.
  6.                                 Mike Kelly
  7.  
  8.         06/12/83 v1.0   written
  9.         07/07/83 v1.0   updated
  10. */
  11.  
  12. /* #define DEBUG 1 */
  13.  
  14. #include "bbscdef.h"
  15.  
  16. #define LASTDATE  " 07/07/83 "
  17.  
  18. #define PGMNAME "BBSCFILE "
  19. #define VERSION " 1.0 "
  20.  
  21. hdrwrt()                /* write the header from memory variables */
  22.         {               /* header is a 1 record file */
  23.         int     fd;
  24.         char    buf128[MSGSECT] ;
  25.  
  26.         if ((fd = open(HEADER,WRITE,0666)) < 0) /* open i/o */
  27.                 {
  28.                 portsout("Can't open header-file, will create it!") ;
  29.                 portsout(CRLF) ;
  30.                 if ((fd = creat(HEADER,0666)) < 0)
  31.                         {
  32.                         portsout("Can't create header-file, aborting!") ;
  33.                         portsout(CRLF) ;
  34.                         return(ERROR) ;
  35.                         }
  36.                 }
  37.         itoa(h_next_msg,h_next) ;       /* convert int to char */
  38.         strfill(buf128,26,MSGSECT) ;    /* init buf128 to all hex 1a */
  39.         sprintf(buf128,"%s~%s~",        /* build record */
  40.                 h_next_msg,
  41.                 h_date) ;
  42.         write(fd,buf128,MSGSECT) ;      /* write it */
  43.         close(fd) ;                     /* no need to leave it open */
  44. #ifdef DEBUG
  45.     portsout(CRLF) ;
  46.     portsout("<<< header file written ok >>>") ;
  47.     portsout(CRLF) ;
  48. #endif
  49.         return(OK) ;
  50.         }
  51.  
  52. hdrread()               /* read the header file into memory */
  53.         {
  54.         int     fd,
  55.                 cnt;
  56.         char    buf128[MSGSECT];
  57.  
  58.         if ((fd = open(HEADER,READ,0666)) < 0)  /* open input */
  59.                 {
  60.                 portsout("Can't open header-file, using inital values!") ;
  61.                 portsout(CRLF) ;
  62.                 h_next = 1 ;
  63.                 h_next_msg[0] = '1' ; h_next_msg[1] = 0 ;
  64.                 h_date[0] = '0' ; h_date[1] = 0 ;
  65.                 hdrwrt() ; return ;
  66.                 }
  67.         if((cnt=read(fd,buf128,MSGSECT)) != MSGSECT)
  68.                 {
  69.                 portsout(CRLF) ;
  70.                 portsout("<<< header read error >>>") ;
  71.                 portsout(CRLF) ;
  72.                 return(ERROR) ;
  73.                 }
  74.         cnt = sscanf(buf128,"%[^~]~%[^~]~",
  75.                         h_next_msg,
  76.                         h_date) ;
  77.         close(fd) ;             /* no need to leave it open */
  78.  
  79. #ifdef DEBUG
  80.     portsout(CRLF) ;
  81.     portsout("<<< header read, next-message='") ;
  82.     portsout(h_next_msg) ;
  83.     portsout("', date='") ;
  84.     portsout(h_date) ;
  85.     portsout("'. >>>") ;
  86.     portsout(CRLF) ;
  87. #endif
  88.  
  89.         if (cnt != 2)
  90.                 {
  91. #ifdef DEBUG
  92.     portsout(CRLF) ;
  93.     portsout("<<< Invalid header read! >>>") ;
  94.     portsout(CRLF) ;
  95. #endif
  96.                 return(ERROR) ;
  97.                 }
  98.         h_next = atoi(h_next_msg) ;
  99.         return(OK) ;
  100.         }
  101.  
  102. msgopen(how)
  103. int     how ;           /* how to open 0=input, 1=output, 2=i/o */
  104.         {
  105.         int     fd ;
  106.  
  107.         if ((fd = open(MESSAGES,how,0666)) < 0) /* open i/o */
  108.                 {
  109.                 portsout("can't open message-file, will create it!") ;
  110.                 portsout(CRLF) ;
  111.                 if ((fd = creat(MESSAGES,0666)) < 0)
  112.                         {
  113.                         portsout("can't create message-file, aborting!") ;
  114.                         portsout(CRLF) ;
  115.                         return(ERROR) ;
  116.                         }
  117.                 }
  118. #ifdef DEBUG
  119.     portsout(CRLF) ;
  120.     portsout("<<< message file opened ok >>>") ;
  121.     portsout(CRLF) ;
  122. #endif
  123.         return(fd) ;
  124.         }
  125.  
  126. msgclose(fd)
  127. int     fd ;
  128.         {
  129. #ifdef DEBUG
  130.     portsout(CRLF) ;
  131.     portsout("<<< closing message file >>>") ;
  132.     portsout(CRLF) ;
  133. #endif
  134.         return(close(fd)) ;
  135.         }
  136.  
  137. msgwrt(fd)              /* write the message file from memory variables */
  138. int     fd;             /* writes a message starting with the h_next msg # */
  139.         {
  140.         int     rc,                     /* return code */
  141.                 cnt1,
  142.                 cnt2,
  143.                 len;
  144.         char    bufmsg0[MSG1MAX+1],
  145.                 buf128[MSGSECT],
  146.                 this1[10],
  147.                 next1[10];
  148.  
  149.         rc = cnt1 = len = cnt2 = 0 ;
  150.         itoa(this1,h_next) ;                /* convert int to char */
  151.         rc = seek(fd,h_next - 1,0) ;        /* seek next available sector */
  152.         h_next++ ;
  153.         itoa(next1,h_next) ;
  154.         strfill(buf128,0,MSGSECT) ;             /* init buf128 to all hex 00 */
  155. /*
  156. *                       build first piece of msg record
  157. */
  158.         sprintf(buf128,"%s~%s~%s~%s~%s~%s~%s~%s~%s~",   /* build record */
  159.                 this1,                                  /* this rcd # */
  160.                 next1,                                  /*  points next rcd # */
  161.                 msg_delete,                             /* delete byte */
  162.                 msg_date,
  163.                 msg_time,
  164.                 msg_to,
  165.                 msg_from,
  166.                 msg_pass,
  167.                 msg_subject);
  168.         rc = write(fd,buf128,MSGSECT);  /* write the first 128 byte record */
  169.                                         /*  for a message record */
  170. /*
  171. *                       build the n+1 piece of msg record
  172. */
  173.  
  174.         len = (strlen(msg_text) / MSG1MAX) + 1; /* calc how many more 128 */
  175.                                                 /*  byte records to write */
  176.         cnt2 = 1 ;                              /* init for substr */
  177.         while (len--)
  178.                 {
  179.                 itoa(this1,h_next);             /* calc/convert record #'s */
  180.                 h_next++;
  181.                 if (len == 0)
  182.                         {
  183.                         strcpy(next1,"0");      /* marks last 128 byte piece */
  184.                         }                       /*  of a msg */
  185.                 else
  186.                         {
  187.                         itoa(next1,h_next);
  188.                         }
  189.                 strfill(bufmsg0,0,MSG1MAX);
  190.                 substr(msg_text,bufmsg0,cnt2,MSG1MAX); /* mv MSG1MAX to buff */
  191.                 cnt2 += MSG1MAX;                /* up cnt2 by MSG1MAX */
  192.                 strfill(buf128,0,MSGSECT);      /* init buf128 to all hex 00 */
  193.                 sprintf(buf128,"%s~%s~%s~%s~",
  194.                         this1,                  /* this rcd # */
  195.                         next1,                  /* point to next rcd # */
  196.                         msg_delete,             /* delete byte */
  197.                         bufmsg0);               /* piece of msg */
  198.                 rc = write(fd,buf128,MSGSECT);  /* write n+1 128 byte record */
  199.                 }
  200.  
  201.         strfill(buf128,26,MSGSECT);             /* fill with all hex 1a */
  202.         rc = write(fd,buf128,MSGSECT);  /* write all hex 1a 128 byte record */
  203.         return(OK);
  204.         }
  205.  
  206. msgrewrt(fd,r_msg)      /* re-write the message file from memory variables */
  207. int     fd,             /* re-writes only the 1st part of a message */
  208.         r_msg;          /* used to update the delete byte */
  209.         {
  210.         int     rc,                     /* return code */
  211.                 cnt1,
  212.                 file_size;
  213.         char    buf128[MSGSECT],
  214.                 this1[10],
  215.                 next1[10];
  216.  
  217.         rc = cnt1 = 0;
  218.         if (r_msg > h_next)     /* don't try to seek past end of file */
  219.                 {
  220.                 return(ERROR);
  221.                 }
  222.         if ((rc = seek(fd,r_msg-1,0)) == ERROR) /* seek to requested sector */
  223.                 {
  224.                 return(ERROR);
  225.                 }
  226.         itoa(this1,r_msg);              /* convert int to char */
  227.         r_msg++;
  228.         itoa(next1,r_msg);
  229.         strfill(buf128,0,MSGSECT);              /* init buf128 to all hex 00 */
  230. /*
  231. *                       build first piece of msg record
  232. */
  233.         sprintf(buf128,"%s~%s~%s~%s~%s~%s~%s~%s~%s~",   /* build record */
  234.                 this1,